package com.dz.client.controller;

import com.dz.client.service.UserProductService;
import com.dz.server.pojo.Product;
import com.dz.server.pojo.User;
import com.dz.server.service.ProductService;
import com.dz.server.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProductManagerController {
    @Autowired
    private UserProductService userProductService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;
    @RequestMapping("/toProductManage")
    public String toProductManage(HttpSession session, Model model){
        String username =(String) session.getAttribute("username");
        User user = userService.selectUserByName(username);
        List<Product> products = userProductService.getProductIds(user.getId());
        if (products.size()==0){
            return "client/no-product-manage";
        }
        model.addAttribute("products",products);
        return "client/product-manage";
    }
    @RequestMapping("/toSeeProduct{productId}")
    public String toSeeProduct(@PathVariable("productId")Integer id,Model model){
        Product product = productService.getById(id);
        model.addAttribute("product",product);
        return "client/see-product";
    }
}
